package cn.exrick.xboot.modules.assessment.controller;

import cn.exrick.xboot.base.XbootBaseController;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.assessment.entity.HkFormEntity;
import cn.exrick.xboot.modules.assessment.entity.HkFormRelationShip;
import cn.exrick.xboot.modules.assessment.service.HkAssessmentIndexService;
import cn.exrick.xboot.modules.assessment.service.HkFormRelationShipService;
import cn.exrick.xboot.modules.assessment.vo.HkAssessmentInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author yck
 */
@Slf4j
@RestController
@Api(description = "表单表单项关联表管理接口")
@RequestMapping("/xboot/hkFormRelationShip")
@Transactional
public class HkFormRelationShipController extends XbootBaseController<HkFormRelationShip, String> {

    @Autowired
    private HkFormRelationShipService hkFormRelationShipService;
    @Autowired
    private HkAssessmentIndexService hkAssessmentIndexService;

    @Override
    public HkFormRelationShipService getService() {
        return hkFormRelationShipService;
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<HkFormRelationShip>> getByCondition(HkFormRelationShip hkFormRelationShip,
                                                            SearchVo searchVo,
                                                            PageVo pageVo){

        Page<HkFormRelationShip> page = hkFormRelationShipService.findByCondition(hkFormRelationShip, searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<Page<HkFormRelationShip>>().setData(page);
    }

    @RequestMapping(value = "/delHkFormRelationShip/{ids}", method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    @ApiOperation(value = "通过id删除")
    public Result delHkFormRelationShip(@PathVariable String[] ids) {
        for (int i = 0; i < ids.length; i++) {
            hkFormRelationShipService.delete(ids[i]);
        }
        return ResultUtil.success("删除成功");
    }

    @RequestMapping(value = "/delHkFormRelationShipByFormEntityId", method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    @ApiOperation(value = "通过表单Id和指标类型删除")
    public Result delHkFormRelationShip(String formEntityId,Integer indexId) {
        List<HkAssessmentInfoVo> byFormEntityIdAndIndexType = hkAssessmentIndexService.findByFormEntityIdAndIndexType(formEntityId, indexId);
        if (byFormEntityIdAndIndexType.size() < 1){
            ResultUtil.error("未找到相关指标项");
        }
        for (int i = 0; i < byFormEntityIdAndIndexType.size(); i++) {
            hkFormRelationShipService.delete(byFormEntityIdAndIndexType.get(i).getId());
        }
        return ResultUtil.success("删除成功");
    }

}
